package com.anytron.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.anytron.model.AppInfo;
import com.anytron.model.req.PageRequest;
import com.anytron.model.res.ResultData;
import com.anytron.service.AppInfoService;
import com.anytron.service.NodeInfoService;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;


@RestController
@RequestMapping(value = "app")
public class AppController {
	
	@Autowired
	AppInfoService appInfoService;
	@Autowired
	NodeInfoService nodeInfoService;
	
	@RequestMapping(value = "list")
	public ResultData<PageInfo<AppInfo>> list(PageRequest pageRequest,AppInfo appInfo) throws Exception {
		ResultData<PageInfo<AppInfo>> resultData = new ResultData<>();
		Example example=new Example(AppInfo.class);
	    Example.Criteria criteria=example.createCriteria();
	    example.setOrderByClause("app_name asc");
	    if (StringUtils.isNotBlank(appInfo.getAppName())) {
	    	criteria.andLike("appName", "%"+appInfo.getAppName()+"%");
		}
		PageInfo<AppInfo> pageInfo = appInfoService.pageByExample(pageRequest, example);
		resultData.setData(pageInfo);
		resultData.setErrorCode(0);
		resultData.setSuccess(true);
		return resultData;
	}
	
	@RequestMapping(value = "queryByAppInfo")
	public ResultData<List<AppInfo>> queryByAppInfo(@RequestBody AppInfo appInfo) throws Exception {
		ResultData<List<AppInfo>> resultData = new ResultData<>();
		List<AppInfo> list = appInfoService.list(appInfo);
		resultData.setData(list);
		resultData.setErrorCode(0);
		resultData.setSuccess(true);
		return resultData;
	}
	
	@RequestMapping(value = "queryNoNodeApp")
	public ResultData<List<AppInfo>> queryNoNodeApp() throws Exception {
		ResultData<List<AppInfo>> resultData = new ResultData<>();
		List<AppInfo> list = appInfoService.queryNoNodeApp();
		resultData.setData(list);
		resultData.setErrorCode(0);
		resultData.setSuccess(true);
		return resultData;
	}
	
	@RequestMapping(value = "add" ,method = RequestMethod.POST)
	public ResultData<String> add(@RequestBody AppInfo appInfo) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		AppInfo one = new AppInfo();
		one.setAppName(appInfo.getAppName());
		AppInfo select = appInfoService.getOne(one);
		if (select!=null) {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("当前输入的应用名称已存在,请重新输入");
			resultData.setSuccess(false);
			return resultData;
		}
		int res = appInfoService.add(appInfo);
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("应用信息保存失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
	
	@RequestMapping(value = "update")
	public ResultData<String> update(@RequestBody AppInfo appInfo) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		AppInfo one = new AppInfo();
		one.setAppName(appInfo.getAppName());
		AppInfo select = appInfoService.getOne(one);
		if (select!=null && select.getId().longValue() != appInfo.getId().longValue()) {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("当前输入的应用名称已存在,请重新输入");
			resultData.setSuccess(false);
			return resultData;
		}
		int res = appInfoService.update(appInfo);
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("应用信息更新失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
	
	@RequestMapping(value = "delete")
	public ResultData<String> delete(@RequestBody AppInfo appInfo) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		int res = appInfoService.delete(appInfo.getId());
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("应用信息删除失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
}
